<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%@ include file="/common/similar.jsp"%>
<%!
	String list(Input input) {
		Integer symptomId = input.getInt("symptom_id", 0);
		Integer type = input.getInt("type", DEPT_EXPERT);
		List<Map<String, Integer>> list = (List<Map<String, Integer>>)(QBuilder.db("basedb").select("new map(detailId as detailId)").from("disease").where(new Where("symId=", symptomId)).query().getResultObj());
		List<Map<String, Object>> disList = new ArrayList<Map<String, Object>>();
		if (list != null) {
			String selectContent = "new map(diseaseName as disease_name,part as part,content as content,medicalDept as medical_dept)";
			for (int i = 0; i < list.size(); i++) {
				List<Map<String, Object>> diseaseList = (List<Map<String, Object>>)(QBuilder.db("basedb").select(selectContent).from("diseaseDetail").where(new Where("id=", list.get(i).get("detailId"))).query().getResultObj());
				if (diseaseList != null) {
					for (int j = 0; j < diseaseList.size(); j++) {
						disList.add(diseaseList.get(j));
					}
				}
			}
		}
		Message msg = new Message(0);
		if (disList.size() == 0) {
			msg = new Message(30041, "没有相关数据");
			msg.put("data", disList);
			return msg.toJson();
		}
		// 查找科室信息
		List<Map> depts = QBuilder.db("basedb").select("new map(id as id , deptId as dept_id, deptName as dept_name, parent as parent, branch as branch, branchName as branch_name, type as register_type)").from("Dept").where(new Where("hospital=", input.getDesc("hospital", "-1")).and("status = ", STATUS_ENABLE).and("type =", type)).order("hospitalOrder", "asc").query().getResultObj();
		Iterator<Map> iterator = depts.iterator();
		List<String> ids = new ArrayList<String>();
		List<String> parents = new ArrayList<String>();
		while (iterator.hasNext()) {
			Map map = iterator.next();
			ids.add(Tools.getDesc(map.get("id")));
			parents.add(Tools.getDesc(map.get("parent")));
		}
		ids.removeAll(parents);
		List<Map> dest = new ArrayList<Map>();
		for (int i = 0; i < ids.size(); i++) {
			String id = ids.get(i);
			for (int j = 0; j < depts.size(); j++) {
				Map map = depts.get(j);
				String _id = Tools.getDesc(map.get("id"), "-------");
				if (_id.equals(id)) {
					dest.add(map);
					break;
				}
			}
		}
		for (int i = 0; i < disList.size(); i++) {
			Map<String, Object> map = disList.get(i);
			String medicalDept = Tools.getDesc(map.get("medical_dept"), "");
			List<Map> suggest = new ArrayList<Map>();
			map.put("suggest", suggest);
			if ("".equals(medicalDept))
				continue;
			List<Map> _list = new ArrayList<Map>();
			_list.addAll(dest);
			for (int j = 0; j < _list.size(); j++) {
				Map _map = _list.get(j);
				String deptName = Tools.getDesc(_map.get("dept_name"), "");
				if ("".equals(deptName))
					continue;
				double s = getSimilarity(medicalDept, deptName);
				if (s > 0.55) {
					_map.put("similar", s+"");
					suggest.add(_map);
				}
			}
			Collections.sort(suggest, new Comparator<Map>(){
			      public int compare(Map n1, Map n2) {
			        return - Tools.getDouble(n1.get("similar")+"", -1.0).compareTo(Tools.getDouble(n2.get("similar")+"", -1.0));
			      }
		    });
		}
		msg.put("data", disList);
		return msg.toJson();
	}
%>
<%
	Tools.encryptPrint(out, list(input));
%>
